package com.xiaocq.wordskiller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class UnitWrongSet extends Activity{
	private static final String TAG = "UnitWrongSet";
	
	private Button backToMenu,switchShow;
	private ListView wrongSet;
	private ProgressDialog progressDialog;
	private long startTimeInMillis,finishTimeInMillis;
	
	private ArrayList<Map<String, String>> list;
	
	private static final String[] FROM = new String[]{
		"unitName",
		"wrongCounts"
	};
	
	private static final int[] TO = new int[]{
		R.id.wrongsetlist_unit,
		R.id.wrongsetlist_wrongCounts
	};
	
	private Handler loadingHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			SimpleAdapter adapter = new SimpleAdapter(UnitWrongSet.this, list, R.layout.wrongsetlist_item, FROM, TO);
			wrongSet.setAdapter(adapter);
			finishTimeInMillis = System.currentTimeMillis();
			Log.d(TAG, "耗时：" + (finishTimeInMillis - startTimeInMillis) + "ms");
			progressDialog.dismiss();
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.unit_wrongset);
		
		prepareInBackground();
		
		initComponents();
	}
	
	private void initComponents(){
		backToMenu = (Button) findViewById(R.id.unit_wrongset_backToMenu);
		switchShow = (Button) findViewById(R.id.unit_wrongset_switchShow);
		wrongSet = (ListView) findViewById(R.id.unit_wrongset_wrongSet);
		
		backToMenu.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				finish();
			}
		});
		
		switchShow.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(UnitWrongSet.this, CustomWrongSet.class);
				startActivity(intent);
				finish();
			}
		});
		
		wrongSet.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent();
				intent.setClass(UnitWrongSet.this, UnitWrongDetail.class);
				intent.putExtra(CommandList.UNIT_ID, position + 1);
				startActivity(intent);
			}
		});
	}
	
	private void prepareInBackground(){
		progressDialog = new ProgressDialog(UnitWrongSet.this);
		progressDialog.setMessage(getText(R.string.tip_loading));
		progressDialog.show();
		
		new Thread(){
			public void run() {
				
				prepareData();
				loadingHandler.sendEmptyMessage(0);
				
			};
		}.start();
	}
	
	private void prepareData(){
		int position;
		int counts;
		startTimeInMillis = System.currentTimeMillis();
		list = new ArrayList<Map<String, String>>();
		for(int i=0; i<50; i++){
			HashMap<String , String> map = new HashMap<String, String>();
			map.put(FROM[0], "List" + (i+1));
			map.put(FROM[1], "-");
			list.add(map);
		}
		
		ActivityHelper helper = new ActivityHelper(UnitWrongSet.this);
		helper.open();
		Cursor cursor = helper.getUnitWrongSetData();
		if(cursor.moveToFirst()){
			do{
				position = cursor.getInt(0);
				counts = cursor.getInt(1);
				Log.d(TAG, "Position " + position + " Counts" + counts);
				list.get(position - 1).put(FROM[1], counts + "个");
			}while(cursor.moveToNext());
		}
		cursor.close();
		helper.close();
	}
}
